myApp.controller('classifiedSalesReport', ['$scope', '$http', '$shop', function($scope, $http, $shop){

	var donutData = [];
	$scope.startDate = "";
	$scope.endDate = "";
	$scope.classId = "";
	$scope.selectedClass = [];
	var allClass = $scope.top_Class;
	var color = ["#C81722", "#EA3EB1", "#5F13D8", "#1E9AB8", "#0F8F3B", "#D6F81A", "#DC7420", "#767070", "#0F0E0E", "#E4DADA"];
	
	$scope.addClass = function(classId) {
		if(findClassByID2(classId)) {
			if($scope.selectedClass.length === 10) {
				alert("最多只能选择10个类型！");
				return;
			}

			$scope.selectedClass.push(findClassByID1(classId));
		}
	};

	$scope.deleteClass = function(classId) {
		for(var i = 0; i < $scope.selectedClass.length; i++) {
			if($scope.selectedClass[i].classId === classId) {
				$scope.selectedClass.splice(i, 1);
				break;
			}
		}
	};

	function findClassByID1(ID) {
		for (var i = 0; i < allClass.length; i++) {
			if(ID === allClass[i].classId) {
				return allClass[i];
			}
		}
	}

	function findClassByID2(ID) {
		for (var i = 0; i < $scope.selectedClass.length; i++) {
			if(ID === $scope.selectedClass[i].classId) {
				return false;
			}
		}
		return true;
	}

	$scope.cancle = function() {
		$scope.startDate = "";
		$scope.endDate = "";
		$scope.classId = "";
		$scope.selectedClass = [];
	};

	$scope.searchClassifiedSalesReport = function() {

		if($scope.startDate === "" || $scope.startDate === null) {
			alert("请选择开始日期！");
			return;
		}
		if($scope.endDate === "" || $scope.endDate === null) {
			alert("请选择结束日期！");
			return;
		}
		if($scope.selectedClass.length === 0) {
			alert("请选择服装类型！");
			return;
		}

		donutData = [];

		var startDate_ = new Date($scope.startDate); 
		var endDate_ = new Date($scope.endDate); 

		var startDate = startDate_.getFullYear() + "-" + (startDate_.getMonth() + 1) 
		+ "-" + startDate_.getDate() + " 00:00:00";
		var endDate = endDate_.getFullYear() + "-" + (endDate_.getMonth() + 1) 
		+ "-" + endDate_.getDate() + " 23:59:59";

		var classId = "";
		for (var i = 0; i < $scope.selectedClass.length; i++) {
			if(i === ($scope.selectedClass.length - 1)) {
				classId += $scope.selectedClass[i].classId;
			}else {
				classId += $scope.selectedClass[i].classId + ",";
			}
		}
		
		var date = {"classId": classId, "startDate": startDate, "endDate": endDate, "shopId": $shop.myShop.shopId};

		var postCfg = {
			headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
			transformRequest: function (data) {
				return $.param(data);
			}
		};

		$http.post('searchClassifiedSalesReport', date, postCfg).then(function successCallback(response) {
			
			var amounts = response.data[0].amounts.split(",");
			for (var i = 0; i < amounts.length; i++) {
				donutData[i] = {};
				donutData[i].label = $scope.selectedClass[i].name;
				donutData[i].data = parseInt(amounts[i]);
				donutData[i].color = color[i];
			}
			showReport();

			console.log("ok：" + response);
		}, function errorCallback(response) {
			alert("查询操作失败!");
			console.log("fail：" + response);
		});

	};

	function showReport() {
		$.plot("#donut-chart", donutData, {
			series: {
				pie: {
					show: true,
					radius: 1,
					innerRadius: 0.5,
					label: {
						show: true,
						radius: 2 / 3,
						formatter: labelFormatter,
						threshold: 0.1
					}

				}
			},
			legend: {
				show: false
			}
		});
	}

	function labelFormatter(label, series) {
		return '<div style="font-size:13px; text-align:center; padding:2px; color: #fff; font-weight: 600;">'
		+ label
		+ "<br>"
		+ Math.round(series.percent) + "%</div>";
	}


}]);